const express = require('express')
const qs = require('querystring')
//路由容器
const store = express.Router()
const axios = require('axios')
const jwt = require("jsonwebtoken")//解析token

// 跳转订单管理页面
store.get('/storeMenu', (req, res) => {
    res.render('admin/storeMenu',{
        userInfo: jwt.decode(req.session.token)
    })
})
// 查询所有店铺
store.get('/findAllShops', (req, res) => {
    let {page,size} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findAllShops',
        method: 'GET',
        params: {
            page: page,
           size: size
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 模糊查询店铺
store.get('/findSuchShops', (req, res) => {
    let {page,size,shopName } = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findSuchShops',
        method: 'GET',
        params: {
            page: page,
            size: size,
            shopName :shopName 
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 根据时间查询店铺
store.get('/findByTime', (req, res) => {
    let {page,size,shopName } = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findByTime',
        method: 'GET',
        params: {
            page: page,
            size: size,
           shopName :shopName 
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 删除单个店铺
store.delete('/deleteAShop', (req, res) => {
    let {userId ,shopName} = req.query;
    console.log("------删除单个店铺-------");
    console.log(req.query);
    axios({
        url: 'consumer/deleteAShop',
        method: 'DELETE',
        params: {
            userId :userId ,
            shopName:shopName  
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 批量删除店铺
store.get('/deleteSuchShops', (req, res) => {
    let {ids} = req.query;
    console.log(req.query);
    for(let key of ids){
        key = parseInt(key);
    }
    console.log(ids);
    axios({
        url: 'consumer/deleteSuchShops',
        method: 'PUT',
        params: {
            ids : ids[0]
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 跳转至店铺审核页面
store.get('/storeInspect', (req, res) => {
    res.render('admin/storeInspect',{
        userInfo: jwt.decode(req.session.token)
    })
})
// 查询所有未审核店铺
store.get('/findAllBussiness', (req, res) => {
    let {page,size} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findAllBussiness',
        method: 'GET',
        params: {
            page: page,
           size: size
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 转至申请信息详情页面
store.get('/storeApply', (req, res) => {
    let {bussiness} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findABussiness',
        method: 'GET',
        params: {
            bussiness :bussiness 
        }
    })
    .then(data => {
        console.log(data.data);
        if(data.data.code==20000){
            res.render('admin/storeApply',{
                data:data.data.data,
                userInfo: jwt.decode(req.session.token)})
        }
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 通过店铺的申请信息
store.post('/passAShop', (req, res) => {
    let {shopName,userId ,introduction } = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/passAShop',
        method: 'POST',
        params: {
            shopName: shopName,
            userId: userId,
            introduction: introduction
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 拒绝店铺的申请信息
store.patch('/refuseAShop', (req, res) => {
    let {bussinessId,reason} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/refuseAShop',
        method: 'PATCH',
        params: {
            bussinessId:bussinessId,
            reason : reason 
            }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 删除一个店铺的申请信息
store.delete('/deleteABussiness', (req, res) => {
    let {bussinessId} = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/deleteABussiness',
        method: 'DELETE',
        params: {
            bussinessId : bussinessId ,
            }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 跳转至店铺评论页面
store.get('/storeComment', (req, res) => {
    let {shopId} = req.query;
    axios({
        url: 'consumer/findCommentById',
        method: 'GET',
        params: {
           shopId :shopId 
        }
    })
    .then(data => {
        res.render('admin/storeComment',{
            shopid: shopId,
            userInfo: jwt.decode(req.session.token)
        })
    })
    .catch(err => {
        console.log("请求失败捕捉错误");
        res.redirect('/admin/error')
    })
})
// 查询店铺所有评论
store.get('/findAllShopComment', (req, res) => {
    let {page,size,shopId } = req.query;
    console.log(req.query);
    axios({
        url: 'consumer/findCommentById',
        method: 'GET',
        params: {
            page: page,
           size: size,
           shopId :shopId 
        }
    })
    .then(data => {
        console.log(data.data);
        res.send({
            data:data.data,
            err:0
        });
    })
    .catch(err => {
        console.log("请求失败捕捉错误");
        // console.log(err.config);
        res.redirect('/admin/error')
    })
})
// 删除单个店铺
store.delete('/deleteAShopComment', (req, res) => {
    let {gCommentId } = req.query;
    console.log("------删除单条评论-------");
    console.log(req.query);
    axios({
        url: 'consumer/deleteCommentById',
        method: 'DELETE',
        params: {
            gCommentId :gCommentId 
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 批量删除店铺
store.get('/deleteShopComments', (req, res) => {
    let {ids} = req.query;
    console.log(req.query);
    for(let key of ids){
        key = parseInt(key);
    }
    console.log(ids);
    axios({
        url: 'consumer/deleteCommentByIds',
        method: 'DELETE',
        params: {
            ids : ids[0]
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
module.exports=store;